package com.titaniume.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @ProjectName: juc
 * @Package: com.titaniume.juc
 * @ClassName: CyclicBarrierDemo
 * @Author: huangrenfei
 * @Description:  循环栅栏 CyclicBarrier  场景: 集齐 7 颗龙珠就可以召唤神龙
 * @Date: 2021/11/23 15:09
 * @Version: 1.0
 */
public class CyclicBarrierDemo {

    //定义神龙召唤需要的龙珠总数
    private final static int NUMBER = 7;

    public static void main(String[] args)  {




        //定义循环栅栏
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER,()->{
            System.out.println("已经集齐7颗龙珠可以召唤神龙了");
        });

        //定义 7 个线程分别去收集龙珠
        for (int i = 1; i <=7; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() + "星龙珠被收集到了");
                try {
                    cyclicBarrier.await();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();

        }
    }
}
